package cn.Aop;

import cn.MetaData.Annotation.ToDo;
import com.alibaba.fastjson.JSONObject;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Aspect
@Slf4j
public class RegisterAop {
    @Pointcut(value = "execution(* cn.Controller.IndexController.proc2(..)))")
    public void proc2Pointcut(){}

    @Pointcut(value = "execution(* cn.Controller.IndexController.oneParamProc(String )) && args( smsVerifyCode))")
    public void oneParamProcPointcut(String smsVerifyCode){}

    @Pointcut(value = "execution(* cn.Controller.IndexController.registerProc(..)) && args(..))")
    public void registerPointcut(){}

    public RegisterAop() {
    }

    @After(value = "proc2Pointcut()")
    public void exampleAfterHandler(){
        System.out.println("进入注册切点....................");
    }

    @Before(value = "oneParamProcPointcut(smsVerifyCode)")
    public void oneParamBeforeHandler(String smsVerifyCode){
        System.out.println("Come in Aop Procduce : "+smsVerifyCode);
    }

    @Before(value = "registerPointcut()")
    public void registerPointcutBeforeHandler(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String smsVerifyCode = (String) args[0];
        HttpServletRequest request = (HttpServletRequest) args[1];
        HttpSession session = request.getSession();

        JSONObject smsVerifyCodeInfo = (JSONObject) session.getAttribute("phoneSmsVerifyCode");
        if(smsVerifyCodeInfo==null){
            request.setAttribute("smsVerifyCodeErrorInfo","#NotHasVerifyCode");
            System.out.println("进入注册切点........SmsVerifyCode : "+smsVerifyCode);
            return;
        }
        System.out.println("进入注册切点........SmsVerifyCode : "+smsVerifyCode);
    }


    /**
     * 注解切点
     * 无参
     */
    @Pointcut(value = "@annotation(cn.MetaData.Annotation.ToDo)")
    public void PointcutAnnotation(){}
    /**
     * 注解切点
     * 带参
     */
    @Pointcut(value = "@annotation(cn.MetaData.Annotation.ToDo) && args(..)" )
    public void PointcutAnnotationWithParameter(){}

    /**
     *
     * 无参
     */
    @Before(value = "PointcutAnnotation()")
    public void oneParamBeforeHandlerByAnno(){
        log.info("$$$$$$$$$$$$$$$$$$$$ 注解切点 无参 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        log.info("Come in Aop Procduce : "+ "powerd by annotation");
        log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }

    /**
     * 带参 注解对象
     * @param dd
     */
    @Before(value = "PointcutAnnotationWithParameter() && @annotation(dd)" , argNames = "dd")
    public void oneParamBeforeHandlerByAnnoWithParam1(ToDo dd ){
        log.info("$$$$$$$$$$$$$$$$$$$$  注解切点 传入注解对象参数  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        log.info("Come in Aop Procduce : "+ "powerd by annotation");
        log.info(dd.status().name());
        log.info(dd.annotationType().getCanonicalName());
        log.info("$$$$$$$$$$$$$$$$$$$$$$$   $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }

    /**
     * 带参 关注点对象
     * @param joinPoint
     */
    @Before(value = "PointcutAnnotationWithParameter()")
    public void oneParamBeforeHandlerByAnnoWithParam2(JoinPoint joinPoint ){
        log.info("$$$$$$$$$$$$$$$$$$$$$$ 注解切点 抓取关注点参数 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        log.info("Come in Aop Procduce : "+ "powerd by annotation" + " JoinPoint");
        String param1 = (String) joinPoint.getArgs()[0];
        log.info(param1);
        Integer param2 = (Integer) joinPoint.getArgs()[1];
        log.info(param1);
        log.info("$$$$$$$$$$$$$$$$$$$$$$ 注解切点 抓取关注点参数 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

    }

}
